﻿using jlynet.core.Library;
using jlynet.core.notebook.myform;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using static System.Windows.Forms.ListViewItem;

namespace jlynet.core.notebook.util
{
    public class ListViewHelper{

        private static ListView listView;
        private static ListViewHelper listViewHelper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="listViewName"></param>
        /// <param name="form"></param>
        public ListViewHelper(string listViewName, Form form)
        {
            if (listView==null)
            {
                listView = getListView(listViewName, form);
            }
        }

        /// <summary>
        /// listView 设置方法
        /// </summary>
        /// <param name="listViewName"></param>
        /// <param name="form"></param>
        public static ListViewHelper setListView(string listViewName, Form form)
        {
            if (listViewHelper==null)
            {
                listViewHelper = new ListViewHelper(listViewName, form);
            }
            return listViewHelper;
        }

        public static ListView getListView(string listViewName, Form form)
        {
            try
            {
                return (ListView)(form.Controls.Find(listViewName, false)[0]);
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 右键复制密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void copyPassword(object sender, EventArgs e)
        {
            ToolStripMenuItem Tsm = (sender as ToolStripMenuItem);
            ToolStripMenuItem item = (ToolStripMenuItem)sender;
            string name = Tsm.Text;

        }

        /// <summary>
        /// 初始化listView数据
        /// </summary>
        public static void listViewData()
        {
            try
            {
                string sql = "select * from entryDataTable";
                //SQLiteConfigHelper.DatabaseFile = SysHelper.GetStartupPath() + "\\222.jlynet";
                DataView dv = MySQLiteHelper.SelectDataView(sql);
                setListViewDatas(dv);
            }
            catch (Exception ex)
            {


            }
        }

        /// <summary>
        /// 查询指定分组下的条目
        /// </summary>
        public void listViewData(string directory_id)
        {
            try
            {
                string sql = "select * from entryDataTable where directory_id = ?";
                DataView dv = MySQLiteHelper.SelectDataView(sql, new SQLiteParameter[] { new SQLiteParameter("directory_id", directory_id) });
                setListViewDatas(dv);
            }
            catch (Exception ex)
            {


            }
        }

        /// <summary>
        /// 模糊查询指定标题下的条目
        /// </summary>
        /// <param name="listView"></param>
        /// <param name="title"></param>
        public static void listViewDataSearch(string title)
        {
            try
            {
                string sql = "select * from entryDataTable where title like ?";
                DataView dv = MySQLiteHelper.SelectDataView(sql, new SQLiteParameter[] { new SQLiteParameter("title", "%" + title + "%") });
                setListViewDatas(dv);
            }
            catch (Exception ex)
            {


            }
        }

        /// <summary>
        /// 绘制ListView
        /// </summary>
        /// <param name="listView"></param>
        /// <param name="dv"></param>
        /// <returns></returns>
        private static void setListViewDatas(DataView dv)
        {
            try{
                if (listView!=null)
                {
                    if(listView.Items.Count>0)
                        listView.Items.Clear();
                    listView.BeginUpdate();   //数据更新，UI暂时挂起，直到EndUpdate绘制控件，可以有效避免闪烁并大大提高加载速度

                    //ImageList imgList = new ImageList();
                    //imgList.ImageSize = new Size(1, 20);// 设置行高 20 //分别是宽和高
                    //listView.SmallImageList = imgList; //这里设置listView的SmallImageList ,用imgList将其撑大

                    //设置listView的图片集
                    //listView.View = View.LargeIcon;
                    //listView.View = View.SmallIcon;
                    ImageList imageList = new ImageList();
                    imageList.Images.Add(UserResource.file);
                    listView.LargeImageList = imageList;

                    int i = 0;
                    foreach (DataRowView row in dv)
                    {
                        i++;
                        ListViewItem lvi = new ListViewItem();
                        lvi.ForeColor = Color.Green;// Color.Green  Color.Red
                        lvi.BackColor = Color.Pink;//Color.Pink Color.LightBlue
                                                   //lvi.ImageIndex = i;     //通过与imageList绑定，显示
                        lvi.ImageIndex = imageList.Images.Count - 1;
                        lvi.Text = row["id_"].ToString();
                        lvi.SubItems.Add(row["title"].ToString());
                        lvi.SubItems.Add(row["username"].ToString());
                        lvi.SubItems.Add(row["password_"].ToString());
                        lvi.SubItems[3].Name = row["password_"].ToString();
                        lvi.SubItems[3].Text = "************";
                        lvi.Tag = row;
                        listView.Items.Add(lvi);
                    }
                    listView.EndUpdate();  //结束数据处理，UI界面一次性绘制。
                }
            }
            catch (Exception ex){

            }
        }

        /// <summary>
        /// 打开选中
        /// </summary>
        /// <param name="listView"></param>
        public static void showSelectedEntry()
        {
            try
            {
                //获取用户当前选择的行的数据
                string name = listView.FocusedItem.SubItems[0].Text;
                name += "             ";
                name += listView.FocusedItem.SubItems[1].Text;
                MessageBox.Show(name);
            }
            catch (Exception ex)
            {


            }
        }

        /// <summary>
        /// 打开条目
        /// </summary>
        /// <param name="listView"></param>
        public static void showEntry()
        {
            try
            {
                foreach (ListViewItem item in listView.Items)
                {
                    for (int i = 0; i < item.SubItems.Count; i++)
                    {
                        MessageBox.Show(item.SubItems[i].Text);
                    }
                }
            }
            catch (Exception ex)
            {


            }
        }

        /// <summary>
        /// 删除选中
        /// </summary>
        /// <param name="listView"></param>
        public void removeEntry()
        {
            try
            {
                foreach (ListViewItem lvi in listView.SelectedItems)
                {//选中项遍历
                    listView.Items.RemoveAt(lvi.Index); // 按索引移除
                    //listView.Items.Remove(lvi);   //按项移除
                }
            }
            catch (Exception ex)
            {


            }
        }

        /// <summary>
        /// 从控件中移除所有项和列（包括列表头）
        /// </summary>
        /// <param name="listView"></param>
        public void listViewClear()
        {
            listView.Clear();  //从控件中移除所有项和列（包括列表头）。
        }

        /// <summary>
        /// 只移除所有的项
        /// </summary>
        /// <param name="listView"></param>
        public void listViewItemsClear()
        {
            listView.Items.Clear();  //只移除所有的项。
        }

        public static Dictionary<string, object> viewDetails(string id)
        {
            try
            {
                string sql = "select * from entryDataTable where id_ = ?";
                DataView dv = MySQLiteHelper.SelectDataView(sql, new SQLiteParameter[] { new SQLiteParameter("id_", id) });
                if (dv != null)
                {
                    Dictionary<string, object> dic = new Dictionary<string, object>();
                    DataRowView row = dv[0];
                    dic["id_"] = row["id_"] != DBNull.Value ? row["id_"] : "";
                    dic["directory_id"] = row["directory_id"] != DBNull.Value ? row["directory_id"] : "";
                    dic["title"] = row["title"] != DBNull.Value ? row["title"] : "";
                    dic["username"] = row["username"] != DBNull.Value ? row["username"] : "";
                    dic["password_"] = row["password_"] != DBNull.Value ? row["password_"] : "";
                    dic["link"] = row["link"] != DBNull.Value ? row["link"] : "";
                    dic["mailbox"] = row["mailbox"] != DBNull.Value ? row["mailbox"] : "";
                    dic["remarks"] = row["remarks"] != DBNull.Value ? row["remarks"] : "";
                    dic["update_time"] = row["update_time"] != DBNull.Value ? row["update_time"] : "";
                    dic["create_time"] = row["create_time"] != DBNull.Value ? row["create_time"] : "";
                    object create_time = row["create_time"];
                    return dic;
                }
            }
            catch (Exception ex)
            {

            }
            return null;
        }

        /// <summary>
        /// 删除条目
        /// </summary>
        /// <param name="listView"></param>
        /// <param name="id"></param>
        public void deleteListView(string id,string directory_id)
        {
            try
            {
                string sql = "delete from entryDataTable where id_= " + id + "";
                MySQLiteHelper.executionSQL(sql);
                listViewData(directory_id);
            }
            catch (Exception ex)
            {


            }
        }

    }
}
